package io.gitee.duxiuhuan.mailbox.database;

import io.gitee.duxiuhuan.mailbox.database.entries.MessageInfo;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface MessageMapper {
    
    @Select("<script>" +
            "SELECT * FROM ${tableName} " +
            "WHERE 1=1 " +
            "<if test='messageInfo.userId != null'>AND user_id = #{messageInfo.userId} </if> " +
            "<if test='messageInfo.sessionId != null'>AND session_id = #{messageInfo.sessionId} </if> " +
            "<if test='messageInfo.userOrSystem != null'>AND user_or_system = #{messageInfo.userOrSystem} </if> " +
            "</script>")
    List<MessageInfo> selectMessage(@Param("tableName") String tableName, @Param("messageInfo") MessageInfo messageInfo);
    
    @Insert({
            "<script>",
            "INSERT INTO ${tableName} (id, user_id, session_id, user_or_system, content, create_time, model) ",
            "VALUES ",
            "<foreach collection='messageInfoList' item='messageInfo' index='index' separator=','>",
            "(#{messageInfo.id}, #{messageInfo.userId}, #{messageInfo.sessionId}, #{messageInfo.userOrSystem}, " +
                    "#{messageInfo.content}, #{messageInfo.createTime}, #{messageInfo.model})",
            "</foreach>",
            "</script>"
    })
    int insertMessages(@Param("tableName") String tableName, @Param("messageInfoList") List<MessageInfo> messageInfoList);
    
    @Delete("DELETE FROM ${tableName} WHERE id = #{id}")
    int deleteMessageById(@Param("tableName") String tableName, @Param("id") Long id);
}

